<?php
	include("$GLOBALS[dir_root]/conf.php");
	include_once("$GLOBALS[dir_class]/CDB.php");
	include_once("$GLOBALS[dir_class]/CUser.php");

	class CCore
	{
		public $app_name;
		private static $_usr;
		private static $_db;
		private static $_debug_content;

		public function __construct($name = "default")
		{
			$this->app_name = $name;
			$this->_debug_content = "";
		}

		public function __destruct()
		{
		}

		public function start(){
			session_start();
			session_destroy();

			if(isset($_SESSION['usr']))
			{
				/* loggato */
				self::$_usr = $_SESSION['usr'];
				$this->_debug("session loaded");
			}
			else
			{
				self::$_usr = new CUser();
				$_SESSION['usr'] = self::$_usr;
				$this->_debug("session registered");
			}

			self::$_db  = new CDB();
			if(!self::$_db->connect(self::$_usr))
			{
				$this->_debug("impossibile connettersi al db", 1);
			}
		}

		public function _debug($s, $lev=0)
		{
			$class = get_class($this);
			$this->debug("[$class]: $s", $lev);
		}

		public static function debug($s, $lev=0)
		{
			switch($lev){
				case 0:	// information
					self::$_debug_content .= "$s\n";
					break;
				case 1: // warning
					self::$_debug_content .= "<b>$s</b>\n";
					break;
				case 2: // error
					self::$_debug_content .= "<font color=\"#FF0000\"><b>$s</b></font>\n";
					break;
				default:// unknow
					self::$_debug_content .= "unknow debug type \"$lev\" for <b>$s</b>\n";
			};
		}

		public function displayDebug()
		{
			return self::$_debug_content;
		}

		public static function getDB()
		{
			return self::$_db;
		}

		public function getUser(){
			return self::$_usr;
		}
	}

?>